﻿using Futaba.SBus.Telemetry;
using Futaba.SBus.Telemetry.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Futaba.SBus.Tests.Telemetry
{
	partial class SlotValueParserTests
	{

		[TestMethod, TestCategory("SBUS2"), TestCategory("SBUS2.Voltage")]
		public void ParseVoltage_Battery()
		{
			var target = SlotValueParser.DefaultInstance;
			var expected = 6.5;
			var parameter = GetParameter(_sensors, SensorType.VoltageSensor, SensorValueType.Volt, "BATTERY");
			var parsingFlags = parameter.ParsingFlags;
			var valueMultiplier = parameter.ValueMultiplyer;

			var ctx = new SlotValueParserContext
			{
				CurrentValue = null,
				ParsingFlags = parsingFlags,
				Data = new byte[] { 0x00, 0x41 },
				ValueMultiplicator = valueMultiplier
			};

			var actual = target.Parse(ctx);
			Assert.AreEqual(expected, actual);
		}

		[TestMethod, TestCategory("SBUS2"), TestCategory("SBUS2.Voltage")]
		public void ParseVoltage_Battery_Strip()
		{
			var target = SlotValueParser.DefaultInstance;
			var expected = 6.1;
			var parameter = GetParameter(_sensors, SensorType.VoltageSensor, SensorValueType.Volt, "BATTERY");
			var parsingFlags = parameter.ParsingFlags;
			var valueMultiplier = parameter.ValueMultiplyer;

			var ctx = new SlotValueParserContext
			{
				CurrentValue = null,
				ParsingFlags = parsingFlags,
				Data = new byte[] { 0x80, 0x3D },
				ValueMultiplicator = valueMultiplier
			};

			var actual = Math.Round(target.Parse(ctx), 2);
			Assert.AreEqual(expected, actual);
		}

		[TestMethod, TestCategory("SBUS2"), TestCategory("SBUS2.Voltage")]
		public void ParseVoltage_Battery_NotNegative()
		{
			var target = SlotValueParser.DefaultInstance;
			var expected = 6.5;
			var parameter = GetParameter(_sensors, SensorType.VoltageSensor, SensorValueType.Volt, "BATTERY");
			var parsingFlags = parameter.ParsingFlags;
			var valueMultiplier = parameter.ValueMultiplyer;

			var ctx = new SlotValueParserContext
			{
				CurrentValue = null,
				ParsingFlags = parsingFlags,
				Data = new byte[] { 0x80, 0x41 },
				ValueMultiplicator = valueMultiplier
			};

			var actual = Math.Round(target.Parse(ctx), 2);
			Assert.AreEqual(expected, actual);
		}

		[TestMethod, TestCategory("SBUS2"), TestCategory("SBUS2.Voltage")]
		public void ParseVoltage_ExtBat()
		{
			var target = SlotValueParser.DefaultInstance;
			var expected = 6.5;
			var parameter = GetParameter(_sensors, SensorType.VoltageSensor, SensorValueType.Volt, "EXT. BATT.");
			var parsingFlags = parameter.ParsingFlags;
			var valueMultiplier = parameter.ValueMultiplyer;

			var ctx = new SlotValueParserContext
			{
				CurrentValue = null,
				ParsingFlags = parsingFlags,
				Data = new byte[] { 0x00, 0x41 },
				ValueMultiplicator = valueMultiplier
			};

			var actual = target.Parse(ctx);
			Assert.AreEqual(expected, actual);
		}

	}
}
